require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
require 'win32ole'
require 'win32/process'


class TC_GroupThemes_test < Test::Unit::TestCase
   
    def startup
      $GroupCategoryName= 'GroupCategory1'
      $GroupCategoryDescription = 'This is the first group category'
      $HubName = 'Group1'
      $AppKey = 'GroupAppKey1'
      $HubDescription = 'This is the first group'
      $CSS ='body{font-size:16px;background-color:white;color:black;}'
      $FilePath1
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
      def test_0000
        require 'win32/process'
        $pid = Process.create(
        :app_name      => 'ruby ../resources/clickers.rb',
        :creation_flags  => Process::DETACHED_PROCESS
        ).process_id

    end  


    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end
      
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToGroupAdministration
        ie.goto($SITE_URL + 'controlpanel/HubAdmin/')
        assert(ie.contains_text(' Group Administration '))      
    end  
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Group Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
    end
      
    def test_10_navigateToAddCategory
        ie.goto($SITE_URL +'controlpanel/HubAdmin/groups.aspx')
        assert(ie.button(:id,/NewGroup$/).exists?)
        ie.button(:id,/NewGroup$/).click
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,/GroupName$/).exists?)
        ie.frame(:index,1).text_field(:id,/GroupName$/).value =$GroupCategoryName
        assert(ie.frame(:index,1).text_field(:id,/GroupDesc$/).exists?)
        ie.frame(:index,1).text_field(:id,/GroupDesc$/).value =$GroupCategoryDescription
        assert(ie.frame(:index,1).link(:id,/SaveButton$/).exists?)
        ie.frame(:index,1).link(:id,/SaveButton$/).click
        sleep 3
      end
    def test_11_navigateToCreateGroup
         ie.goto($SITE_URL+'groups/')
        sleep 3
        assert(ie.link(:text,'Create a new group').exists?)
        ie.link(:text,'Create a new group').click
        sleep 3
        assert(ie.text_field(:id,/HubName$/).exists?)
        ie.text_field(:id,/HubName$/).value =$HubName
        $HubName1 =  $HubName.downcase
        sleep 3
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_bcr_bcr_ctl00_ctl02_HubDescription_ctl00_Editor').execCommand('mceSetContent', false,'"+$HubDescription+"')")
        sleep 3
        assert(ie.radio(:id,/HubType_0$/).exists?)
        assert(ie.radio(:id,/HubType_1$/).exists?)
        assert(ie.radio(:id,/HubType_2$/).exists?)
        ie.radio(:id,/HubType_0$/).set
        assert(ie.select_list(:id,/HubGroup$/).exists?)
        sleep 2
        ie.select_list(:id,/HubGroup$/).select($GroupCategoryName)
        assert(ie.button(:id,/Create$/).exists?)
        ie.button(:id,/Create$/).click
    end

    def test_12_navigateToThemes
        ie.goto($SITE_URL+'groups/'+$HubName1 +'/default.aspx')
        assert(ie.link(:text,'Edit this Theme').exists?)
        ie.link(:text,'Edit this Theme').click
        assert(ie.select_list(:id,/Themes_state$/).exists?)
        ie.select_list(:id,/Themes_state$/).select('Lean and Green')
        # Sidebar
        # Group Widgets
        assert(ie.select_list(:id,/ConfigurationForm_ctl08_cfoptions_state$/).exists?)
        ie.select_list(:id,/ConfigurationForm_ctl08_cfoptions_state$/).select('Group List')
        assert(ie.button(:id ,/ConfigurationForm_ctl08_addButton$/).exists?)
        ie.button(:id ,/ConfigurationForm_ctl08_addButton$/).click
        sleep 2
        assert(ie.select_list(:id,/ConfigurationForm_ctl08_cfoptions_state$/).exists?)
        ie.select_list(:id,/ConfigurationForm_ctl08_cfoptions_state$/).select('Group Members List')
        assert(ie.button(:id ,/ConfigurationForm_ctl08_addButton$/).exists?)
        ie.button(:id ,/ConfigurationForm_ctl08_addButton$/).click
        sleep 2
        assert(ie.select_list(:id,/ConfigurationForm_ctl08_cfoptions_state$/).exists?)
        ie.select_list(:id,/ConfigurationForm_ctl08_cfoptions_state$/).select('Widgetbox Widget')
        assert(ie.button(:id ,/ConfigurationForm_ctl08_addButton$/).exists?)
        ie.button(:id ,/ConfigurationForm_ctl08_addButton$/).click
        sleep 5
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.SelectItem(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.GetItemCount()-1,true)")
        assert(ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_up').exists?)
        ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_up').fire_event "onclick"
        sleep 2
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.SelectItem(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.GetItemCount()-2,true)")
        assert(ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_up').exists?)
        ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_up').fire_event "onclick"
        sleep 2
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.SelectItem(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.GetItemCount()-3,true)")
        assert(ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_up').exists?)
        ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_up').fire_event "onclick"
        sleep 2
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.SelectItem(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.GetItemCount()-4,true)")
        assert(ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_up').exists?)
        ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_up').fire_event "onclick"
        sleep 2
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.SelectItem(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.GetItemCount()-3,true)")
        assert(ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_down').exists?)
        ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_down').fire_event "onclick"
        sleep 2
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.SelectItem(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.GetItemCount()-2,true)")
        assert(ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_down').exists?)
        ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_down').fire_event "onclick"
        sleep 2
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.SelectItem(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist.GetItemCount()-1,true)")
        assert(ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_down').exists?)
        ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_down').fire_event "onclick"
        sleep 2
        assert(ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_delete').exists?)
        ie.button(:id,'ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl08_cflist_delete').fire_event "onclick"
         
        # Custom Styles (Advanced)
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl00.SelectTab(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl00.GetTabAtIndex(1))")
        sleep 4
        assert(ie.text_field(:name,/ctl03$/).exists?)
        sleep 4
        ie.text_field(:name,/ctl03$/).set($CSS)
        # Preview
        # This is giving a random error
        #~ ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl00.SelectTab(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl00.GetTabAtIndex(2))")
        #~ sleep 2
        #~ assert(ie.button(:value,'Start Preview').exists?)
        #~ ie.button(:value,'Start Preview').click
        #~ sleep 2
        #~ ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl00.SelectTab(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl00.GetTabAtIndex(2))")
        #~ sleep 20
       #~ #These are not working as expected 
        #~ assert(ie.button(:value,'Update Preview').exists?)
        #~ ie.button(:value,'Update Preview').click
        #~ sleep 20
        #~ ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl00.SelectTab(ctl00_ctl00_ctl00_bcr_bcr_bcr_ctl00_ctl05_ConfigurationForm_ctl00.GetTabAtIndex(2))")
        #~ assert(ie.button(:value,'End Preview').exists?)
        #~ ie.button(:value,'End Preview').click
        sleep 5
        #~ # Export
        #~ assert(ie.button(:value,'Export').exists?)
        #~ ie.button(:value ,'Export').click
        #Import Themes
        sleep 5
        assert(ie.button(:value,'Import').exists?)
        ie.button(:value,'Import').click
        sleep 5
        assert(ie.frame(:index,1).fileField(:id,/File$/).exists?)
        ie.frame(:index,1).fileField(:id,/File$/).set($FilePath1)
        sleep 5
        assert(ie.frame(:index,1).button(:id,/Submit$/).exists?)
        ie.frame(:index,1).button(:id,/Submit$/).click
         # RestoreDefaults
        assert(ie.button(:id,/RestoreDefaults$/).exists?)
        ie.button(:id,/RestoreDefaults$/).click
         # Save
        assert(ie.button(:value,'Save').exists?)
        ie.button(:value,'Save').click
      end
      
       def test_13_DeleteGroup
        sleep 5
         ie.goto($SITE_URL+'controlpanel/HubAdmin/hubs.aspx')
        assert(ie.link(:text,'All').exists?)
        ie.link(:text,'All').click
        assert(ie.table(:id,'BlogLists').exists?)
        table1 = ie.table(:id, 'BlogLists')
        rowcount = table1.row_count
        rowcount1 =1
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$HubName1).exists?)
              ok =1
               sleep 5
               table1[rowcount1][6].link(:text, 'Delete').click
               break
             end   
            rowcount1=rowcount1+1
        end
      end
      
       def test_14_navigateToDeleteCategory
        ie.goto($SITE_URL+'controlpanel/HubAdmin/groups.aspx')
        sleep 5
        assert(ie.hidden(:id,'ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_CategoriesTree').exists?)
        hiddenvalue=ie.hidden(:id,'ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_CategoriesTree').value
        hiddenvaluearr=hiddenvalue.split('&')
        groupid=hiddenvaluearr[hiddenvaluearr.length-1].split('=')
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_CategoriesTree.SelectNode(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_CategoriesTree.GetNodeByValue("+groupid[1]+"))")
        sleep 5
        assert(ie.button(:value ,'Delete').exists?)
        ie.button(:value ,'Delete').click
      end
      
    def test_99_Exit
      ie.close
      end
    
end
       